package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.Complain;
import cn.edu.ujn.shi.dao.Msg;
import cn.edu.ujn.shi.service.IComplainService;
import cn.hutool.core.date.DateUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 投诉表 前端控制器
 *
 * @author LiChao
 * @since 2024-05-31
 */
@Controller
public class ComplainController {

    @Autowired
    private IComplainService complainService;

    //普通用户列表页面方法
    @RequestMapping("complainList")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();
            List<Complain> list = complainService.getComplain(username);
            request.setAttribute("list", list);

        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }


        return "ComplainList";
    }


    //管理员投诉管理
    @RequestMapping("complainAdminList")
    public String complainAdminList(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();
            String complainant = request.getParameter("complainant");
            List<Complain> list = complainService.getComplainLike(complainant);
            request.setAttribute("list", list);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return "ComplainAdminList";
    }

    @RequestMapping("/addComplain")
    public String addComplain() {
        return "AddComplain";
    }

    @RequestMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();
            String complainantContent = request.getParameter("complainantContent");
            complainService.insert(new Complain(username, complainantContent, DateUtil.now()));
            response.sendRedirect("complainList");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @RequestMapping("/updateComplain")
    public String updateComplain(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("id", request.getParameter("id"));
        request.setAttribute("complainantContent", complainService.getComplainById(request.getParameter("id")).getContent());
        return "UpdateComplain";
    }

    @RequestMapping("/updateComplainAdmin")
    public String updateComplainAdmin(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("id", request.getParameter("id"));
        Complain complain = complainService.getComplainById(request.getParameter("id"));
        request.setAttribute("complainantContent", complain.getContent());
        request.setAttribute("feedback", complain.getFeedback());
        return "UpdateComplainAdmin";
    }

    @RequestMapping("/update")
    public void update(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();
            //用是否传了回复来区分管理员和用户
            if (request.getParameter("feedback") == null) {
                //普通用户
                String complainantContent = request.getParameter("complainantContent");
                String id = request.getParameter("id");
                complainService.updateComplain(new Complain(Integer.valueOf(id), username, complainantContent, DateUtil.now(), "", "", ""));
                response.sendRedirect("complainList");
            } else {
                //管理员
                String feedback = request.getParameter("feedback");
                String id = request.getParameter("id");
                complainService.updateComplain(new Complain(Integer.valueOf(id), null, null, null, username, feedback, DateUtil.now()));
                response.sendRedirect("complainAdminList");

            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @RequestMapping("/DeleteComplainServlet")
    public void deleteMsgServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(request.getParameter("id"));
            complainService.delete(id);
            response.sendRedirect("complainList");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
